{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "88df4c34",
   "metadata": {},
   "source": [
    "## python面试题\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1acde4fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 1, 2: 2, 3: 3}\n"
     ]
    }
   ],
   "source": [
    "# 60.\n",
    "from typing import List, Dict\n",
    "def count_dict(lst:List) -> Dict:\n",
    "    dct = {}\n",
    "    for num in lst:\n",
    "        if isinstance(num, int):\n",
    "            dct[num] = dct.get(num, 0) + 1 \n",
    "    return dct\n",
    "\n",
    "testData = [1, 2, 2, 3, 3, 3]\n",
    "\n",
    "testResult = count_dict(testData)\n",
    "print(testResult)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77dd4823",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 61\n",
    "import os\n",
    "\n",
    "dir_path = '../week3'\n",
    "for dir, file in os.walk(dir_path):\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06448fc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 62\n",
    "def finial_ways(two, three, five, total):\n",
    "    count = 0\n",
    "    two_count = int(total / two)\n",
    "    three_count = int(total / three)\n",
    "    five_count = int(total / five)\n",
    "\n",
    "    for i in range(1, five_count + 1):\n",
    "        for j in range(1, three_count + 1):\n",
    "            for k in range(1, two_count + 1):\n",
    "                if total == i * five + j * three + k * two:\n",
    "                    count += 1\n",
    "                    # print(i, j, k)\n",
    "\n",
    "    return count\n",
    "\n",
    "result = finial_ways(2, 3, 5, 99)\n",
    "print(result)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7daa015",
   "metadata": {},
   "source": [
    "### 63:阅读代码，写出程序运行结果\n",
    "![63题](../img/week4_img/63.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8d0a7ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 4]\n",
      "[1, 3]\n",
      "[('a', 1), ('b', 2), ('c', 3), ('d', 4)]\n",
      "{2: 'item4', 4: 'item16', 6: 'item36'}\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "# 63\n",
    "items = [1, 2, 3, 4]\n",
    "print([i for i in items if i > 2])  # 3, 4\n",
    "print([i for i in items if i % 2])  # 1, 3\n",
    "print([(x, y) for x ,y in zip('abcd', (1, 2, 3, 4, 5))])      # [(a, 1) (b, 2) (c, 3) (d, 4)]\n",
    "\n",
    "# 这里出错，要细心\n",
    "print({x: f\"item{x ** 2}\" for x in (2, 4, 6)})      # item4, item16, item 36 xxx\n",
    "\n",
    "# 这个结果也错了 - 1. 该表达式属于集合推导式  2. x不在‘abcdefg’中才计入统计，这里我理解错了\n",
    "print(len({x for x in 'hello world' if x not in 'abcdefg'}))    # x: e, d -> len(x) = 2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "000f444f",
   "metadata": {},
   "source": [
    "### 64:说出代码的运行结果\n",
    "类属性的继承\n",
    "![类属性的继承](../img/week4_img/64.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc32b8f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 64\n",
    "class Parent:\n",
    "    x = 1\n",
    "\n",
    "class Child1(Parent):\n",
    "    pass\n",
    "\n",
    "class Child2(Parent):\n",
    "    pass\n",
    "\n",
    "print(Parent.x, Child1.x, Child2.x)     # 1, 1 1\n",
    "Child1.x = 2\n",
    "\n",
    "print(Parent.x, Child1.x, Child2.x)     # 1, 2, 1\n",
    "Parent.x = 3\n",
    "\n",
    "print(Parent.x, Child1.x, Child2.x)     # 3, 2, 3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd264d43",
   "metadata": {},
   "source": [
    "### 65 \n",
    "python库中常用的模块：\n",
    "os, re, time, math, json, \n",
    "\n",
    "这道题还是不太熟悉"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e2ff0cf",
   "metadata": {},
   "source": [
    "### 66\n",
    "__init__ 在对象创建之后调用，为类的属性赋值，而__new__在对象创建之前进行调用，负责在内存中为对象生成开辟空间"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9b88d5f",
   "metadata": {},
   "source": [
    "### 67\n",
    "静态工具分析：在vscode中使用pylint进行静态分析"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "391464af",
   "metadata": {},
   "source": [
    "### 68\n",
    "魔术方法：__del__ 对象删除语句， __res__ 对象提示语句，__init__ 为实例的属性进行自动赋值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a60783ea",
   "metadata": {},
   "source": [
    "### 69 \n",
    "\n",
    "这道题目还可以深挖args和kwargs的实现机制\n",
    "\n",
    "*arg代表可变参数，它通常用于函数传入参数不确定数量的情况下； **kwargs表示关键字参数，它通常会赋予用户对函数功能实现更精细化的调整，例如在数据可视化绘制图表的相关函数，往往会提供这样一种接口，方便用户进行定制化绘图。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "82e06f4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# 69\n",
    "def print_args(*args):\n",
    "    for i in args:\n",
    "        print(i)\n",
    "\n",
    "print_args(1, 2, 3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9663999f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "key:A, value:a\n",
      "key:A, value:d\n",
      "key:B, value:b\n",
      "key:B, value:b\n",
      "key:C, value:c\n",
      "key:C, value:c\n"
     ]
    }
   ],
   "source": [
    "# 69\n",
    "def print_kwargs(dit: Dict,**kwargs):\n",
    "\n",
    "    for key, value in dit.items():\n",
    "        print(f\"key:{key}, value:{dit[key]}\")\n",
    "        if key in kwargs.keys():\n",
    "            dit[key] = kwargs[key]\n",
    "        print(f\"key:{key}, value:{dit[key]}\")\n",
    "\n",
    "dit = {\"A\":'a', \"B\":'b', \"C\":'c'}\n",
    "\n",
    "print_kwargs(dit, A = 'd')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaa1d906",
   "metadata": {},
   "source": [
    "### 70\n",
    "变量的作用域有三种：1.全局变量 2.局部变量（函数内部 - 类内部） 3.函数空间变量\n",
    "\n",
    "作用实现作用域的隔离，防止命名空间的污染"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7c8ac649",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# 70\n",
    "\n",
    "a = 0\n",
    "def func():\n",
    "    a = 1\n",
    "    print(a)\n",
    "    \n",
    "func()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f53f700c",
   "metadata": {},
   "source": [
    "### 71\n",
    "\n",
    "闭包（Closure） 是 Python 中一个非常有用的特性，它允许函数捕获并“记住”其外部作用域中的变量，即使这些变量所在的作用域已经结束。使用闭包有以下几个重要原因：\n",
    "\n",
    "1. 减少全局变量的使用\n",
    "闭包可以帮助我们避免使用全局变量，从而使代码更加模块化和安全。通过将变量封装在闭包中，可以限制其作用范围，避免意外修改。\n",
    "示例\n",
    "```python\n",
    "def make_multiplier(factor):\n",
    "    def multiplier(number):\n",
    "        return number * factor\n",
    "    return multiplier\n",
    "\n",
    "double = make_multiplier(2)\n",
    "print(double(5))  # 输出 10\n",
    "```\n",
    "\n",
    "这里，factor 被封装在闭包中，外部无法直接访问或修改它。\n",
    "\n",
    "2. 实现数据的持久化（状态保持）\n",
    "闭包可以用来保存函数的状态信息，而不需要使用类或对象。它非常适合需要记住某些数据的场景。\n",
    "示例：\n",
    "\n",
    "```Python\n",
    "def counter():\n",
    "    count = 0\n",
    "    def increment():\n",
    "        nonlocal count\n",
    "        count += 1\n",
    "        return count\n",
    "    return increment\n",
    "\n",
    "counter_instance = counter()\n",
    "print(counter_instance())  # 输出 1\n",
    "print(counter_instance())  # 输出 2\n",
    "```\n",
    "这里，count 的值在闭包中被持久化，每次调用 increment 都会更新状态。\n",
    "\n",
    "3. 用作回调函数\n",
    "闭包可以作为回调函数，尤其是在异步编程或事件处理场景中，闭包可以携带额外的上下文信息。\n",
    "示例：\n",
    "```Python\n",
    "def create_callback(message):\n",
    "    def callback():\n",
    "        print(f\"Callback says: {message}\")\n",
    "    return callback\n",
    "\n",
    "cb = create_callback(\"Hello, World!\")\n",
    "cb()  # 输出 \"Callback says: Hello, World!\"\n",
    "```\n",
    "\n",
    "4. 提高代码的简洁性和可读性\n",
    "闭包可以让代码更加简洁，避免使用复杂的类定义，同时实现类似的功能。\n",
    "\n",
    "总结\n",
    "闭包的核心优势在于：\n",
    "\n",
    "封装性：将数据和逻辑绑定在一起。\n",
    "灵活性：可以动态创建函数并携带上下文信息。\n",
    "简洁性：减少代码冗余，提升可读性。\n",
    "\n",
    "在实际开发中，闭包常用于装饰器、回调函数、状态管理等场景，是 Python 编程中不可或缺的工具！\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76aad9b9",
   "metadata": {},
   "source": [
    "### 72\n",
    "打印函数：在python2中‘print’可以不用加括号，但在python3中将其视作函数，必须按照函数的用法添加括号引用\n",
    "在对可迭代对象进行遍历时，python2 中range和rangx并存，但是python3同一使用range\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ea119f8",
   "metadata": {},
   "source": [
    "### 73\n",
    "猴子补丁是指在程序运行过程中，依赖注入的方式将为函数在先用功能之外添加新的功能特性。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
